<?php
/**
 * product_categories
 * Author: lyun
 * Date: 2018/1/25
 * Created by Panxsoft.
 */

namespace App\Http\Models\Admin;

use App\Observers\BosCleanObserver;
use App\Services\Utils\Bosable;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;

/**
 * @property int super_id
 * @property ProductCategory[]|\Illuminate\Database\Eloquent\Collection subCategories
 * @property-write ProductCategory[]|\Illuminate\Database\Eloquent\Collection sub_categories
 * @property ProductCategory[]|Collection bladeNavSubCategories
 * @property bool is_more
 * @method self filterTopLevel()
 */
class ProductCategory extends Model
{
    use Bosable;

    const TOP_CATEGORY = 0;  // 一级的种类

    public $bos_columns = ['thumb'];

    protected $bos_key_prefix = 'product_category/thumb/';

    protected $table = 'product_categories';

    protected $guarded = [];

    protected $casts = [
        'super_id' => 'int',
    ];

    /**
     *  商品种类 ==> 商品
     *
     * @return \Illuminate\Database\Eloquent\Relations\HasMany|Product
     */
    public function products()
    {
        return $this->hasMany(Product::class, 'product_category_id', 'id');
    }

    /**
     *  一级种类 ==> 二级种类
     *
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function subCategories()
    {
        return $this->hasMany(ProductCategory::class, 'super_id', 'id');
    }

    /**
     * blade 导航需要用到的二级菜单
     *
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function bladeNavSubCategories()
    {
        return $this->hasMany(ProductCategory::class, 'super_id', 'id')
            ->latest('priority');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function getSubCategories()
    {
        return $this->subCategories()->latest('priority');
    }

    /**
     *  返回一级种类和顶级种类的数组
     *
     * @return array
     */
    public static function getSupCategories()
    {
        $super_categories = self::where('super_id', ProductCategory::TOP_CATEGORY)
                ->pluck('name', 'id')
                ->all()+[0 => '顶级类别'];

        return $super_categories;
    }

    /**
     *  判断是否是二级种类
     * @return ProductCategory|Model|null
     */
    public function judgeIsSubCategory()
    {
        return $this->where('super_id', '<>', self::TOP_CATEGORY)->first();
    }

    /**
     *  检测是否有二级的种类
     *
     * @return null|self
     */
    public function checkHasSubCategories()
    {
        return $this->subCategories()->first();
    }

    /**
     *  检测种类下面是否有商品
     *
     * @return Product|null
     */
    public function checkHasProduction()
    {
        return $this->products()->first();
    }

    // Query Scopes BEGIN
    public function scopeFilterTopLevel(Builder $builder)
    {
        return $builder->where('super_id', 0);
    }
    // Query Scopes END

    /**
     * @param $thumb
     */
    public function setThumbAttribute($thumb)
    {
        if (is_file($thumb)) {
            $this->attributes['thumb'] = $this->uploadPostFile($thumb , $this->bos_key_prefix);
        }
    }

    /**
     * @return mixed|null
     */
    public function getThumbAttribute()
    {
        if (! $this->attributes['thumb']) return null;

        return $this->getStaticUrl($this->attributes['thumb']);
    }

    /**
     *  注册删除bosKey事件绑定
     *
     */
    protected static function boot()
    {
        parent::boot();

        /*
         * 监听bos清理事件
         */
        self::observe((BosCleanObserver::class));
    }

}
